﻿using System.Collections.Generic;
using UnityEngine;

public enum UIName:byte
{
    MainPanel,
    GamePanel,
    InstallPanel,
    SkinPanel,
    AdvertisingPanel,
    GameOverOnePanel,
    GameOverTwoPanel,
    GameOverThreePanel
}

public class UIManager:Singleton<UIManager>
{
    Dictionary<UIName, UIBase> dic = new Dictionary<UIName, UIBase>();
    public void OpenUI(UIName name)
    {
        if (dic.ContainsKey(name))
        {
            dic[name].Open();
        }
        else
        {
            LoadUI(name).Open();
        }
    }

    private UIBase LoadUI(UIName name)
    {
        UIBase ui = null;
        if (dic.ContainsKey(name))
        {
            ui = dic[name];
        }
        else
        {
            ui = Resources.Load<UIBase>("UI/Panel/"+name.ToString());
            if (ui != null)
            {
                ui = GameObject.Instantiate(ui, GameObject.Find("UIRoot").transform);
                ui.Init();
                dic.Add(name, ui);
            }
        }
        return ui;
    }
    public void CloseUI(UIName name)
    {
        if (dic.ContainsKey(name))
        {
            dic[name].Close();
        }
    }
    public void UNLoadUI(UIName name)
    {
        if (dic.ContainsKey(name))
        {
            dic.Remove(name);
        }
    }
    public UIBase GetUI(UIName name)
    {
        if (dic.ContainsKey(name))
        {
            return dic[name];
        }
        else
        {
            Debug.Log("空面板");
            return null;
        }
    }
}
